<?php

class PageManagerController extends eCMSModuleController {
	
	/**
	 * 
	 */
	public function index() {
		
		//Get all Areas
		$AreaList = new AreaList();
		$AreaList->GetAllItems();
		$this->addViewData("Areas", $AreaList);
		
		//If an Area is selected, load Pages from this Area
		if($this->getPageRequest()->getGetVar("fkArea")) {
			
			//Get Page List by the fkArea
			$pageList = new PageList();
			$pageList->GetByFkArea($this->getPageRequest()->getGetVar("fkArea"));
			$this->addViewData("Items", $pageList);
			
		}
		
		
	}

	/**
	 * 
	 */
	public function edit() {
		
	}

	
}

/*
class PageManager extends Page {
	
	private $items, $areas;
	
	public function __construct($pkPage) {
		parent::__construct($pkPage);
	}
	
	public function assignCustomDataToSmarty(Smarty $s) {
		//Wird vor dem Render aufgerufen
		$s->assign("PageManager_Items", $this->items);
		$s->assign("PageManager_Areas", $this->areas);
	}
	
	
	
	public function index() {
		
		$cfg = Runtime::getConfiguration();
		
		if(isset($_GET["fkArea"])) {
			$this->items = PageItemDeprecated::getByArea($_GET["fkArea"]);
		}
		//Get Areas
		$sql = "SELECT a.*, d.* FROM " .$cfg['table_prefix'] . "Area AS a 
				LEFT JOIN " .$cfg['table_prefix'] . "Design AS d ON d.pkDesign = a.fkDesign";
		
		$this->areas = ResultItem::getAllByQuery($sql);
	
	}
	
}

class PageItemDeprecated extends ResultItem {
	
	private $page;
	private $PageFactory;
	
	public function __construct($fkPage) {
		
		parent::__construct(null);
		
		if(!isset($this->PageFactory)) {
			$this->PageFactory = new PageFactory();
		}
		
		try {
			$this->page = $this->PageFactory->CreatePage($fkPage);
		}
		catch (Exception $e) {}	
			
		if($this->page) {
			//echo "load page '" .$this->page->getTitle() . "'<br>";
			$this->SecurityObject = $this->page->getPageSecurityObject();
		}
		else {
			$this
			//trigger_error("Page not found!");
		}
	}
	
	public function getPageTitle() {
		if($this->page) return $this->page->getTitle();
		return "";
	}

	public function canOpenEditor() {
		if($this->page) return ($this->page->canInstanceUpdate() && $this->page->canInstanceRead() && $this->isEditable);
		return false;
	}
	
	public static function getSubPageItems($ref, $level=0) {
		
		$conn = MySQLConnection::getInstance();
		$cfg = Runtime::getConfiguration();
		
		$items = array();
		
		$sql = "SELECT %i as 'level', p.*, m.name AS 'moduleName', m.isEditable AS 'isEditable', mi.name AS 'moduleInstanceName', mi.pkModuleInstance FROM tblPage AS p 
				LEFT JOIN " .$cfg['table_prefix'] . "ModuleInstance AS mi on mi.pkModuleInstance = p.fkModuleInstance
				LEFT JOIN " .$cfg['table_prefix'] . "Module AS m ON m.pkModule = mi.fkModule 
				";
		
		
		//$sql = "SELECT %i as 'level', p.*, m.name AS 'moduleName' FROM " .$cfg['table_prefix'] . "Page AS p 
		//		LEFT JOIN " .$cfg['table_prefix'] . "ModuleInstance AS mi on mi.pkModuleInstance = p.fkModuleInstance
		//		LEFT JOIN " .$cfg['table_prefix'] . "Module AS m on mi.fkModule = mi.fkModule "; 
		
		
		if($level==0) {
			//Check Top-Pages under the Selected Area
			$sql.= "WHERE p.fkArea = %i ";
		}
		else {
			$sql.= "WHERE p.fkParentPage = %i ";
		}
		$sql.="ORDER BY sortID ASC";
		
		$res = $conn->query($sql, array($level, $ref));
		$i=0;
		$last=null;
		while(($data = mysql_fetch_array($res))==true) {
			$i++;
			//Set Own Properties
			
			$pi = new PageItemDeprecated($data["pkPage"]);
			$pi->loadData($data);
			
			if($pi->canUpdate()) {
				$data["canMoveUp"] = isset($last);
				$data["canMoveDown"] = $i < mysql_numrows($res);
				$data["canMoveLeft"] = $level>0;
				$data["canMoveRight"] = $last["level"] == $level;
			}
			
			$pi->loadData($data);
			
			array_push($items, $pi);
			
			//Add SubItems
			$subitems = self::getSubPageItems($data["pkPage"],$level+1);
			if($subitems) { $items = array_merge($items, $subitems); }

			$last = $data;
			
		}

		
		return $items;
		
	}
	
	public static function getByArea($pkArea) {
		return self::getSubPageItems($pkArea);

	}
}
*/
?>